import {
    reqBuyerResgister, reqBuyerLogin, reqBuyerInfo, reqUpdateBuyerDetail,
    reqUpdateBuyerPassword, reqChangeDefaultAddress, reqChangeAddress
} from "@/api";
import { setToken, getToken, removeToken } from '@/utils/token';
const state = {
    token: getToken(),
    buyerInfo: {}
};
const mutations = {
    BUYERLOGIN(state, token) {
        state.token = token;
    },
    GETBUYERINFO(state, buyerInfo) {
        state.buyerInfo = buyerInfo;
    },
    CLEAR(state) {
        //清除仓库中用户信息和本地Token
        state.token = '';
        state.buyerInfo = {};
        removeToken();
    }
};
const actions = {
    //用户注册
    async buyerRegister({ commit }, buyer) {
        let result = await reqBuyerResgister(buyer);
        if (result.code == 200) {
            alert(result.data);
            return 'ok';
        } else {
            return Promise.reject(new Error('faile'));
        }
    },
    //登录业务(token)
    async buyerLogin({ commit }, data) {
        let result = await reqBuyerLogin(data);
        //服务器下发的token,用户的唯一标识符
        if (result.code == 200) {
            commit('BUYERLOGIN', result.data);
            //如果返回了token则持久化存储token
            if (result.data) {
                setToken(result.data);
                alert("登录成功");
            } else {
                alert("账号或密码错误");
            }
            //localStorage.setItem("TOKEN",result.data.token);
            return 'ok';
        } else {
            return Promise.reject(new Error('faile'));
        }
    },
    //获取用户信息
    async getBuyerInfo({ commit }) {
        let result = await reqBuyerInfo();
        if (result.code == 200) {
            if (result.data) {
                commit('GETBUYERINFO', result.data);
                return 'ok';
            } else {
                return null;
            }
        } else {
            return Promise.reject(new Error('faile'));
        }
    },
    //退出登录
    async buyerLogout({ commit }) {
        commit('CLEAR');
    },
    //修改用户信息
    async updateBuyerDetail({ commit }, data) {
        let result = await reqUpdateBuyerDetail(data);
        if (result.code == 200) {
            commit('BUYERLOGIN', result.data);
            //更新token
            setToken(result.data);
            return 'ok';
        } else {
            return Promise.reject(new Error('faile'));
        }
    },
    //修改用户密码
    async updateBuyerPassword({ commit }, { oldBuyerPw, buyerPw }) {
        let result = await reqUpdateBuyerPassword(oldBuyerPw, buyerPw);
        if (result.code == 200) {
            alert(result.data);
            return 'ok';
        } else {
            return Promise.reject(new Error('faile'));
        }
    },
    //修改默认地址
    async changeDefaultAddress({ commit }, address) {
        let result = await reqChangeDefaultAddress(address);
        if (result.code == 200) {
            commit('BUYERLOGIN', result.data);
            //更新token
            setToken(result.data);
            return 'ok';
        } else {
            return Promise.reject(new Error('faile'));
        }
    },
    //修改买家地址
    async changeAddress({ commit }, data) {
        let result = await reqChangeAddress(data);
        if (result.code == 200) {
            commit('BUYERLOGIN', result.data);
            //更新token
            setToken(result.data);
            return 'ok';
        } else {
            return Promise.reject(new Error('faile'));
        }
    }
};
const getters = {};
export default {
    state, mutations, actions, getters
}